package com.yiren.dbaa.module.practice_set.map.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.line.view.adapter.BindLineAdapter;
import com.yiren.dbaa.module.practice_set.map.contract.BindLineContract;
import com.yiren.dbaa.module.practice_set.map.presenter.BindLinePresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Describe 绑定线路的页面
 *
 * @author WRX
 * @date 2022/12/23
 */
public class BindLineActivity extends BaseActivity implements BindLineContract.View, BindLineAdapter.OnLineSelectListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_line_info)
    ListView lvLineInfo;

    private BindLineContract.Presenter bindLinePresenter;
    private List<Object> lineInfoList;

    private Intent intent;
    private MapEntity map;

    /*** 选中的线路 */
    private Map<Integer, LineInfoEntity> checkLineInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_line);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        tvTitle.setText("绑定线路");
        bindLinePresenter = new BindLinePresenter(this);
        lineInfoList = new ArrayList<>();
        checkLineInfos = new HashMap<>();
        intent = getIntent();
        map = (MapEntity) intent.getSerializableExtra("map");
        bindLinePresenter.loadUserAllLine(map.getSiteCode(), this);
    }

    @Override
    public void getLineInfoSuccess(List<Object> lineInfoList) {
        if (null != lineInfoList && lineInfoList.size() > 0) {
            this.lineInfoList = lineInfoList;
            BindLineAdapter adapter = new BindLineAdapter(this, map, lineInfoList, this);
            lvLineInfo.setAdapter(adapter);
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_bind_line})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_bind_line: //绑定
                if (checkLineInfos.size() == 0) {
                    MessageBox.show(this, "请至少选择一条绑定的线路！");
                    return;
                }
                List<LineInfoEntity> lines = new ArrayList<>();
                for (Map.Entry<Integer, LineInfoEntity> entry : checkLineInfos.entrySet()) {
                    lines.add(entry.getValue());
                }
                Intent intent = new Intent(this, MapManageActivity.class);
                intent.putExtra("lines", (Serializable) lines);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void onLineSelect(boolean selected, int position) {
        if (selected) {
            checkLineInfos.put(position, (LineInfoEntity) lineInfoList.get(position));
        } else {
            checkLineInfos.remove(position);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}